/*
 * StartovniKategorieBrowseDialog.java
 *
 * Created on 2. leden 2007, 12:11
 */

package cz.results.winter6.view.dialogs.stkategorie;

import cz.results.winter6.model.KategorieConfig;
import cz.results.winter6.model.KategorieConfigList;
import cz.results.winter6.model.slmodel.StartListModel;
import java.util.Iterator;
import java.util.TreeMap;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;

public class StartovniKategorieBrowseDialog extends JDialog {
    
    private StartovniKategorieBrowseTableModel tm;
    private StartListModel startListModel;
    
    public StartovniKategorieBrowseDialog(JDialog parent, boolean modal, StartListModel startListModel) {
        super(parent, modal);
        setStartListModel(startListModel);
        setTm(new StartovniKategorieBrowseTableModel(getStartovniKategorieConfigMap()));
        initComponents();
        naplnJComboBox();
    }
    
    /** naplni JComboBox nazvama typu startovni kategorie (kvalifikace, finale, apod...) */
    public void naplnJComboBox(){
        jComboBox1.setModel(new DefaultComboBoxModel());
        
        Iterator iterator = getStartovniKategorieConfigMap().keySet().iterator();
        while(iterator.hasNext()){
            String nazevTypuStartovniKaegorie = (String) iterator.next();
            ((DefaultComboBoxModel)jComboBox1.getModel()).addElement(nazevTypuStartovniKaegorie);
        }
    }
    
    
    private void upravaKategorie(){
        final JDialog parent = this;
        
        if(jTable1.getSelectedRow()!=-1) {                            //musi byt vybran nejaky radek
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new StartovniKategorieDialog(parent, true, getStartListModel(), getTm().getNazevTypuStartovniKategorie(), jTable1.getSelectedRow(), false).setVisible(true);
                    getTm().fireTableDataChanged();
                }
            });
        }
    }
    
    public StartovniKategorieBrowseTableModel getTm() {
        return tm;
    }
    
    public void setTm(StartovniKategorieBrowseTableModel tm) {
        this.tm = tm;
    }
    
    public StartListModel getStartListModel() {
        return startListModel;
    }
    
    public void setStartListModel(StartListModel startListModel) {
        this.startListModel = startListModel;
    }
    
    private TreeMap<String,KategorieConfigList> getStartovniKategorieConfigMap(){
        return getStartListModel().getZavod().getStartovniKategorieConfigMap();
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItemNaplnVsem = new javax.swing.JMenuItem();
        jMenuItemVymazVsem = new javax.swing.JMenuItem();
        jComboBox1 = new javax.swing.JComboBox();
        jButtonEditTypStKategorie = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButtonNovy = new javax.swing.JButton();
        jButtonUprav = new javax.swing.JButton();
        jButtonSmaz = new javax.swing.JButton();
        jButtonMoveUp = new javax.swing.JButton();
        jButtonMoveDown = new javax.swing.JButton();
        jButtonZrus = new javax.swing.JButton();

        jMenuItemNaplnVsem.setText("napl\u0148 v\u0161em");
        jMenuItemNaplnVsem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemNaplnVsemActionPerformed(evt);
            }
        });

        jPopupMenu1.add(jMenuItemNaplnVsem);

        jMenuItemVymazVsem.setText("vyma\u017e v\u0161em ");
        jMenuItemVymazVsem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemVymazVsemActionPerformed(evt);
            }
        });

        jPopupMenu1.add(jMenuItemVymazVsem);

        setTitle("konfigurace startovn\u00edch kategori\u00ed");
        setLocationByPlatform(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        getAccessibleContext().setAccessibleParent(null);
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jButtonEditTypStKategorie.setText("uprav");
        jButtonEditTypStKategorie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditTypStKategorieActionPerformed(evt);
            }
        });

        jTable1.setModel(tm);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTable1MousePressed(evt);
            }
        });

        jScrollPane1.setViewportView(jTable1);

        jButtonNovy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/edit_add.png")));
        jButtonNovy.setText("nov\u00fd");
        jButtonNovy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNovyActionPerformed(evt);
            }
        });

        jButtonUprav.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/edit.png")));
        jButtonUprav.setText("uprav");
        jButtonUprav.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUpravActionPerformed(evt);
            }
        });

        jButtonSmaz.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/edit_remove.png")));
        jButtonSmaz.setText("sma\u017e");
        jButtonSmaz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSmazActionPerformed(evt);
            }
        });

        jButtonMoveUp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/1uparrow.png")));
        jButtonMoveUp.setText("up");
        jButtonMoveUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMoveUpActionPerformed(evt);
            }
        });

        jButtonMoveDown.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/1downarrow.png")));
        jButtonMoveDown.setText("down");
        jButtonMoveDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonMoveDownActionPerformed(evt);
            }
        });

        jButtonZrus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/results/winter6/view/icons/button_cancel.png")));
        jButtonZrus.setText("zru\u0161");
        jButtonZrus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonZrusActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 637, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonEditTypStKategorie))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jButtonNovy)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonUprav)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSmaz)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                        .addComponent(jButtonMoveUp, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(14, 14, 14)
                        .addComponent(jButtonMoveDown)
                        .addGap(25, 25, 25)
                        .addComponent(jButtonZrus)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonEditTypStKategorie))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonNovy)
                        .addComponent(jButtonUprav)
                        .addComponent(jButtonSmaz))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonZrus)
                        .addComponent(jButtonMoveDown)
                        .addComponent(jButtonMoveUp)))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemVymazVsemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemVymazVsemActionPerformed
        getTm().deleteValueInAllRows(jTable1.getSelectedColumn());
    }//GEN-LAST:event_jMenuItemVymazVsemActionPerformed
    
    private void jTable1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MousePressed
        if(evt.getButton()==evt.BUTTON3){                   //prave tlacitko mysi
            
            //vyberu prislusnou bunku v tabulce kam se kleplo pravym tlacitkem mysi
            jTable1.setRowSelectionInterval(jTable1.rowAtPoint(evt.getPoint()), jTable1.rowAtPoint(evt.getPoint()));
            jTable1.changeSelection(jTable1.rowAtPoint(evt.getPoint()),jTable1.columnAtPoint(evt.getPoint()),false,false);
            
            if(getTm().isPopupMenuEnable(jTable1.getSelectedColumn())) {        //kdyz je pro dany sloupecek popupmenu povoleno
                jPopupMenu1.show(evt.getComponent(), evt.getX(), evt.getY());   //zobrazim poupmenu
            }
        }
    }//GEN-LAST:event_jTable1MousePressed
    
    private void jMenuItemNaplnVsemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNaplnVsemActionPerformed
        getTm().copyValueToAllRows(jTable1.getSelectedRow(),jTable1.getSelectedColumn());
    }//GEN-LAST:event_jMenuItemNaplnVsemActionPerformed
    
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        getTm().setNazevTypuStartovniKategorie((String)jComboBox1.getSelectedItem());
        getTm().fireTableStructureChanged();
    }//GEN-LAST:event_jComboBox1ActionPerformed
    
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if(evt.getClickCount()==2){
            upravaKategorie();
        }
    }//GEN-LAST:event_jTable1MouseClicked
    
    private void jButtonEditTypStKategorieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditTypStKategorieActionPerformed
        final JDialog parent = this;
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TypyStartovniKategorieDialog(parent, true, getStartovniKategorieConfigMap()).setVisible(true);
                naplnJComboBox();
            }
        });
    }//GEN-LAST:event_jButtonEditTypStKategorieActionPerformed
    
    private void jButtonZrusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonZrusActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_jButtonZrusActionPerformed
    
    private void jButtonMoveDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMoveDownActionPerformed
        int selectedRow = jTable1.getSelectedRow();
        
        if(selectedRow > -1 && selectedRow < getTm().getRowCount()-1) {                            //musi byt vybran nejaky radek
            KategorieConfig kategorieConfig = getTm().getKategorieConfigList().get(selectedRow);
            getTm().getKategorieConfigList().remove(kategorieConfig);
            getTm().getKategorieConfigList().add(selectedRow+1,kategorieConfig);
            getTm().fireTableDataChanged();
            jTable1.setRowSelectionInterval(selectedRow+1, selectedRow+1);
        }
    }//GEN-LAST:event_jButtonMoveDownActionPerformed
    
    private void jButtonMoveUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonMoveUpActionPerformed
        int selectedRow = jTable1.getSelectedRow();
        
        if(selectedRow > 0) {                            //musi byt vybran nejaky radek
            KategorieConfig kategorieConfig = getTm().getKategorieConfigList().get(selectedRow);
            getTm().getKategorieConfigList().remove(kategorieConfig);
            getTm().getKategorieConfigList().add(selectedRow-1,kategorieConfig);
            getTm().fireTableDataChanged();
            jTable1.setRowSelectionInterval(selectedRow-1, selectedRow-1);
        }
    }//GEN-LAST:event_jButtonMoveUpActionPerformed
    
    private void jButtonSmazActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSmazActionPerformed
        if(jTable1.getSelectedRow()!=-1) {                            //musi byt vybran nejaky radek
            getTm().getKategorieConfigList().remove(jTable1.getSelectedRow());
            getTm().fireTableDataChanged();
        }
    }//GEN-LAST:event_jButtonSmazActionPerformed
    
    private void jButtonUpravActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUpravActionPerformed
        upravaKategorie();
    }//GEN-LAST:event_jButtonUpravActionPerformed
    
    private void jButtonNovyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNovyActionPerformed
        final JDialog parent = this;
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StartovniKategorieDialog(parent, true, getStartListModel(),getTm().getNazevTypuStartovniKategorie(),0, true).setVisible(true);
                getTm().fireTableDataChanged();
            }
        });
    }//GEN-LAST:event_jButtonNovyActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonEditTypStKategorie;
    private javax.swing.JButton jButtonMoveDown;
    private javax.swing.JButton jButtonMoveUp;
    private javax.swing.JButton jButtonNovy;
    private javax.swing.JButton jButtonSmaz;
    private javax.swing.JButton jButtonUprav;
    private javax.swing.JButton jButtonZrus;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JMenuItem jMenuItemNaplnVsem;
    private javax.swing.JMenuItem jMenuItemVymazVsem;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
    
}
